<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Services\FleaGoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FleaGoodsController extends Controller
{

    protected $service;

    public function __construct(FleaGoodsService $fleaGoodsService)
    {
        $this->service = $fleaGoodsService;
    }


    /**
     * @Notes 添加闲置
     * @param Request $request
     * @return array
     * @author ypp
     * @date 2021/3/8
     * @time 10:40
     */
    public function store(Request $request): array
    {
        $data = $request->post();
        //数据验证
        $validator = Validator::make($data, [
            'goods_name'    => 'required',
            'goods_content'    => 'required',
            'goods_images' => 'required',
            'address'    => 'required',
            'goods_price'  => 'required',
            'cate_id'  => 'required',
            'deal_type'  => 'required'
        ], [
            'goods_name.required'      => '请输入名称',
            'goods_content.required'      => '请输入描述',
            'goods_images.required'   => '请上传图片',
            'address.required'   => '请输入地址',
            'goods_price.required'    => '请填写价格',
            'cate_id.required'    => '请选择分类',
            'deal_type.required'    => '请选择交易方式',
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        $info = $this->service->addFleaGoods($data);
        return $info['status'] ? $this->success([]) : $this->error(['msg']);
    }

    /**
     * @Notes 用户的闲置列表
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:38
     */
    public function index(): array
    {
        $info = $this->service->getFealGoodsListByUserId();
        return $info['status'] ? $this->success($info['data']) : $this->error(['msg']);
    }

    /**
     * @Notes 编辑
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:38
     */
    public function edit(Request $request): array
    {
        $data = $request->input();
        $info = $this->service->edit($data);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * @Notes 更改卖出状态
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:38
     */
    public function update_sale($id): array
    {
        $info = $this->service->saleGoods($id);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * @Notes 获取详情
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:43
     */
    public function show($id)
    {
        $info = $this->service->detail($id, 0);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * @Notes 删除
     * @param $ids
     * @return array
     * @author ypp
     * @date 2021/3/9
     * @time 10:30
     */
    public function destroy ($id)
    {
        $info = $this->service->delete($id);
        return $info['status'] ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * @Notes 获取商品列表
     * @return array
     * @author ypp
     * @date 2021/3/16
     * @time 14:54
     */
    public function getFleaGoods()
    {
        $info = $this->service->getFleaGoodsByType();
        return $info['status'] ? $this->success($info['data']) : $this->error(['msg']);
    }
}
